package com.qyhl.webtv.module_news.news.jlnews.list;


import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

public class JLNewsListModel implements JLNewsListContract.JLNewsListModel {
    private JLNewsListPresenter mPresenter;

    JLNewsListModel(JLNewsListPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getList(int type, final String page) {
        String url;
        if (type == 0) {
            url = NewsUrl.JL_NEWS_PROVINCE_LIST;
        } else {
            url = NewsUrl.JL_NEWS_TOWN_LIST;
        }

        EasyHttp.get(url)
                .params("page", page)
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError(0, !page.equals("1"));
                        } else {
                            mPresenter.setError(1, !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        if (newsBeans != null && newsBeans.size() > 0) {
                            mPresenter.setListData(newsBeans, !page.equals("1"));
                        } else {
                            mPresenter.setError(0, !page.equals("1"));
                        }
                    }
                });

    }
}
